package com.sen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sen.dto.RoleDTO;
import com.sen.entity.Resource;
import com.sen.entity.Role;
import com.sen.mapper.ResourceMapper;
import com.sen.mapper.RoleMapper;
import com.sen.result.Result;
import com.sen.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.management.Query;
import java.sql.Wrapper;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sen
 * @since 2021-03-29
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private ResourceMapper resourceMapper;

    /**
     * 获取角色列表
     * @param cur 第几页
     * @param size 每页记录数
     * @return
     */
    @Override
    public Result getRoleList(Integer cur, Integer size) {
        Page<Role> page = new Page<>(cur, size);
        Page<Role> rolePage = roleMapper.selectPage(page, null);
        List<Role> roles = rolePage.getRecords();
        return Result.ok().data("data", roles);
    }

    /**
     * 新增一个角色
     * @param role
     * @return
     */
    @Override
    public Result saveRole(Role role) {
        boolean b = this.save(role);
        if (b){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    /**
     * 根据角色id删除角色
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result deleteRoleById(Integer id) {
        // 删除对应的角色记录
        boolean b = this.removeById(id);
        // 删除角色资源表中该角色拥有的资源记录
        boolean b1 = roleMapper.deleteResourcesById(id);
        if (b) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 更新角色信息，不包括角色拥有的资源信息
     * @param role
     * @return
     */
    @Override
    public Result updateRole(Role role) {
        int i = roleMapper.updateById(role);
        if (i > 0){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    /**
     * 更新角色拥有的资源信息
     * @param roleId
     * @param resourceIdList
     * @return
     */
    @Override
    public Result updateRoleResource(Integer roleId, List<Integer> resourceIdList) {
        boolean b = roleMapper.deleteResourcesById(roleId);
        boolean b1 = roleMapper.insertRoleResource(roleId, resourceIdList);
        if (b && b1) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 根据角色ID查询角色的详细信息，包括该角色拥有的资源信息
     * @param id
     * @return
     */
    @Override
    public RoleDTO getRoleById(Integer id) {
        List<Resource> resources = resourceMapper.getResourceByRoleId(id);
        Role role = roleMapper.getRoleById(id);
        RoleDTO roleDTO = new RoleDTO();
        BeanUtils.copyProperties(role, roleDTO);
        roleDTO.setResources(resources);
        return roleDTO;
    }
}
